Event.observe ( window, 'load', bootstrap );

function bootstrap () {
  $( 'deliverable_name' ).observe ( 'mouseout', update_version_dropdown_list );
  $( 'no_description'   ).on      ( 'change',   no_description_changed       );
  
  var deliverable_name    = $( 'deliverable_name'    ).value;
  var deliverable_version = $( 'deliverable_version' ).value;
  
  if ( deliverable_name != '' && deliverable_version != '' ) {
    display_issues ();
  }
}

function update_version_dropdown_list () {
  var deliverable_name    = $( 'deliverable_name' ).value;
  var select_id           = 'deliverable_version';
  
  new Ajax.Request ( '/rest/dstp/deliverable/' + deliverable_name + '/versions', {
    method         : 'get',
    requestHeaders : { Accept : 'application/json' },
    onComplete     : function ( transport ) {
      var versions = transport.responseJSON;

      var html =
        "<select id=\"" + select_id + "\" name=\"" + select_id + "\">\n"
      + "  <option value=\"\">-</option>\n"
      ;

      versions.each ( function ( version ) {
        html = html + "  <option value=\"" + version + "\">" + version + "</option>\n";
      } );

      html = html
      + "</select>\n"
      ;

      $( select_id ).replace ( html );
      
      $( 'deliverable_version' ).on ( 'change', display_issues );
    }
  } );
}

function display_issues () {
  var deliverable_name    = $( 'deliverable_name'    ).value;
  var deliverable_version = $( 'deliverable_version' ).value;
  
  if ( deliverable_name != '' && deliverable_version != '' ) {
    new Ajax.Request ( '/rest/dstp/deliverable/' + deliverable_name + '/version/' + deliverable_version + '/issues', {
      method         : 'get',
      requestHeaders : { Accept : 'application/json' },
      onCreate       : function () {
        $( 'issues_table' ).replace ( '<div id="issues_table"></div>' );
      },
      onFailure      : function () {
        $( 'issues_table' ).replace ( '<p id="issues_table" class="error">No issues!</p>' );
      },
      onSuccess      : function ( transport ) {
        var issues = transport.responseJSON;

        if ( issues.length > 0 ) {
          $( 'issues_table' ).replace ( issues_as_html_table ( issues, 'issues_table', 'alone' ) );
          
          no_description_changed ();

          TableKit.Sortable.init ( 'issues_table' );
          TableKit.reload ();
        }
        else {
          $( 'issues_table' ).replace ( '<p id="issues_table" class="error">No issues!</p>' );
        }
      },
      onComplete     : function () {
        display_commits ();
      }
    } );
  }
}

function display_commits () {
  var deliverable_name    = $( 'deliverable_name'    ).value;
  var deliverable_version = $( 'deliverable_version' ).value;
  
  if ( deliverable_name != '' && deliverable_version != '' ) {
    new Ajax.Request ( '/rest/datasheet/' + deliverable_name + '/commits', {
      method         : 'get',
      requestHeaders : { Accept : 'application/json' },
      onCreate       : function () {
        $( 'commits_table' ).replace ( '<div id="commits_table"></div>' );
        
        display_progress ( 'Querying SVN...' );
      },
      onFailure      : function () {
        $( 'commits_table' ).replace ( '<p id="commits_table" class="error">No commits!</p>' );
      },
      onSuccess      : function ( transport ) {
        var commits = transport.responseJSON;

        if ( commits.length > 0 ) {
          $( 'commits_table' ).replace ( commits_as_html_table ( commits, 'commits_table', deliverable_name, deliverable_version, 'number' ) );
        }
        else {
          $( 'commits_table' ).replace ( '<p id="commits_table" class="error">No commits!</p>' );
        }
      },
      onComplete     : function () {
        hide_progress ();
      }
    } );
  }
}
